package com.outsourcing.bottle.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.outsourcing.bottle.domain.PushNoticeInfo;

/**
 * 推送信息表
 * 
 * @author 06peng
 * 
 */
public class PushNoticeInfoTable {

	private final String PUSH_NOTICE_TABLE = "push_notice_info";

	private final String LOGIN_UID = "login_uid";

	private DBManager db = null;

	public PushNoticeInfoTable() {
		if (db == null) {
			db = DBManager.get();
		}
		if (!db.isTableExits(db.getConnection(), PUSH_NOTICE_TABLE)) {
			createBottleType();
		}
	}
	
	public PushNoticeInfoTable(Context context) {
		if (db == null) {
			db = DBManager.get(context);
		}
		if (!db.isTableExits(db.getConnection(), PUSH_NOTICE_TABLE)) {
			createBottleType();
		}
	}

	private void createBottleType() {
		String createSql = "create table if not exists "
				+ PUSH_NOTICE_TABLE
				+ " (id integer primary key autoincrement,"
				+ LOGIN_UID + " integer, new_notice_count integer, message_content varchar, message_avatar varchar,"
				+ " new_message_count integer, new_btfeed_count integer, new_exfeed_count integer, new_maybeknow_count integer)";
		db.creatTable(db.getConnection(), createSql, PUSH_NOTICE_TABLE);
	}

	public void savePushNoticeInfo(PushNoticeInfo pushInfo, int login_uid) {
		ContentValues values = new ContentValues();
		values.put(LOGIN_UID, login_uid);
		values.put("new_notice_count", pushInfo.getNew_notice_count());
		values.put("message_content", pushInfo.getMessage_content());
		values.put("message_avatar", pushInfo.getMessage_avatar());
		values.put("new_message_count", pushInfo.getNew_message_count());
		values.put("new_btfeed_count", pushInfo.getNew_btfeed_count());
		values.put("new_exfeed_count", pushInfo.getNew_exfeed_count());
		values.put("new_maybeknow_count", pushInfo.getNew_maybeknow_count());
		db.save(db.getConnection(), PUSH_NOTICE_TABLE, values);
	}

	/**
	 * 根据ID删除
	 * */
	public void deletePushNoticeByLoginUid(int login_uid) {

		String deleteSql = LOGIN_UID + "=? ";
		db.delete(db.getConnection(), PUSH_NOTICE_TABLE, deleteSql,
				new String[] { String.valueOf(login_uid) });
		db.closeConnection(db.getConnection());
	}

	public PushNoticeInfo getPushNoticeInfo(int login_uid) {
		Cursor cursor = null;
		PushNoticeInfo pushInfo = null;
		try {
			cursor = db.find(db.getConnection(), "select * from "
					+ PUSH_NOTICE_TABLE + " where " + LOGIN_UID + "=?",
					new String[] { String.valueOf(login_uid) });
			if (cursor != null) {
				while (cursor.moveToNext()) {
					pushInfo = new PushNoticeInfo();
					pushInfo.setMessage_avatar(cursor.getString(cursor.getColumnIndex("message_avatar")));
					pushInfo.setMessage_content(cursor.getString(cursor.getColumnIndex("message_content")));
					pushInfo.setNew_btfeed_count(cursor.getInt(cursor.getColumnIndex("new_btfeed_count")));
					pushInfo.setNew_exfeed_count(cursor.getInt(cursor.getColumnIndex("new_exfeed_count")));
					pushInfo.setNew_message_count(cursor.getInt(cursor.getColumnIndex("new_message_count")));
					pushInfo.setNew_notice_count(cursor.getInt(cursor.getColumnIndex("new_notice_count")));
					pushInfo.setNew_maybeknow_count(cursor.getInt(cursor.getColumnIndex("new_maybeknow_count")));
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (cursor != null && !cursor.isClosed()) {
				cursor.close();
			}
		}
		return pushInfo;
	}

	public void clearTable() {
		db.delete(db.getConnection(), PUSH_NOTICE_TABLE, null, null);
	}
	
	public void updatePushInfo(String type, String value, String login_uid) {
		try {
			String sql = "update table "+PUSH_NOTICE_TABLE+" set "+type+" = "+value+" where "+LOGIN_UID+" = ?";
			db.update(db.getConnection(), sql, new String[] {login_uid} );
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
